import java.util.HashMap;
import java.util.Random;
import java.util.Vector;

/**
 * Class holding the structure of the whole environment (RoadMap).
 * @author Jared Lindbl�m
 *
 */
public class RoadMap {
	private HashMap<String, Intersection> intersections;
	private HashMap<String, Road> roads;
	private Vector<Vehicle> vehicles;
	private String name;

	/**
	 * Constructor. We probably won't use this, but it allows us to 
	 * specify the initial size of the hash-maps.
	 * @param name				The name of the road.
	 * @param numIntersections	The number of intersections in the map.
	 * @param numRoads			The number of roads in the map.
	 */
	public RoadMap(String name, int numIntersections, int numRoads) {
		this.name = name;
		intersections = new HashMap<String, Intersection>(numIntersections);
		roads = new HashMap<String, Road>(numRoads);
		vehicles = new Vector<Vehicle>();
	}

	/**
	 * Constructor.
	 * @param name	The name of the road.
	 */
	public RoadMap(String name) {
		this.name = name;
		intersections = new HashMap<String, Intersection>();
		roads = new HashMap<String, Road>();
		vehicles = new Vector<Vehicle>();
	}

	/**
	 * Accessor Method (Name).
	 * @return	The name of the Road Map.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Accessor Method (Intersection).
	 * @param name	The name of the intersection to get.
	 * @return 	The pointer to the object of the intersection requested.
	 */
	public Intersection getIntersection(String name) {
		return intersections.get(name);
	}

	/**
	 * Accessor Method (Road).
	 * @param name	The name of the road to get.
	 * @return	The pointer to the object of the road requested.
	 */
	public Road getRoad(String name) {
		return roads.get(name);
	}

	/**
	 * Accessor Method (Vehicles).
	 * @return An array of vehicles in the RoadMap.
	 */
	public Vehicle[] getVehicles() {
		return (Vehicle[]) vehicles.toArray();
	}

	/**
	 * Adds an intersection to the Road Map.
	 * @param name				The name of the intersection (key).
	 * @param newIntersection	The new intersection (value).
	 */
	public void addIntersection(String name, Intersection newIntersection) {
		intersections.put(name, newIntersection);
	}

	/**
	 * Adds a road to the Road Map.
	 * @param name		The name of the road (key).
	 * @param newRoad	The new road (value).
	 */
	public void addRoad(String name, Road newRoad) {
		roads.put(name, newRoad);
	}

	/**
	 * Adds a vehicle to the RoadMap randomly
	 * @param name	The name of the vehicle to be created.
	 */
	public void addVehicleRandomly(String name) {
		Random random = new Random();
		Intersection[] interList = new Intersection[intersections.size()];
		(intersections.values()).toArray(interList);

		vehicles.addElement(new Vehicle(name, interList[random.nextInt(interList.length)], interList[random.nextInt(interList.length)], 30 + random.nextInt(20)));
	}

	/**
	 * Prints out the structure of the Road Map (for debugging).
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String print = "RoadMap: " + name + "\n\n";
		String printHold = "";

		print = print.concat("\tRoads: (" + roads.size() + " in total)\n\n");

		print = print.concat("\t\tNorth/South:\n\n");
		for (Road road: roads.values()) {
			if (road.getType().equals("North/South"))
				print = print.concat("\t\t\t" + road.toString() + "\n");
			else {
				printHold = printHold.concat("\t\t\t" + road.toString() + "\n"); 
			}
		}

		print = print.concat("\n\t\tEast/West:\n\n" + printHold + "\n");

		print = print.concat("\tIntersections: (" + intersections.size() + " in total)\n\n");
		for (Intersection intersection: intersections.values()) {
			print = print.concat("\t\t" + intersection.toString() + "\n");
		}

		print = print.concat("\n\tVehicles: (" + vehicles.size() + " in total)\n\n");
		for (int i = 0; i < vehicles.size(); i++) {
			print = print.concat("\t\t" + (vehicles.get(i)).toString() + "\n");
		}

		print = print.concat("(end)");
		return print;
	}
}